package cn.wzg.netbase;

import static java.net.HttpURLConnection.HTTP_OK;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

import cn.wzg.netbase.utils.StreamUtil;

public class GetHtmlActivity extends AppCompatActivity {
    private String link="https://campus.zzuli.edu.cn/portal-pc/pc/qy/index.html";
    private TextView tv_html;
    static class MyHandler extends Handler {
        WeakReference<Activity> activityWeakReference;

        //构造函数传入外部的this
        public MyHandler(@NonNull Looper looper, Activity mainActivity) {
            super(looper);
            //构建一个弱引用对象
            activityWeakReference = new WeakReference<Activity>(mainActivity);
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            //判断活动是否还存在，如果不存在则结束
            GetHtmlActivity activity = (GetHtmlActivity) activityWeakReference.get();
            if (activity == null){
                return;
            }
            String str= (String) msg.obj;
            activity.tv_html.setText(str);//显示Html
        }
    }
    private Handler handler=new MyHandler(Looper.myLooper(),this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_get_html);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        tv_html=findViewById(R.id.textView2);
        //新开线程进行网络访问
        new Thread(){
            @Override
            public void run() {
                try {
                    URL url=new URL(link);//创建URL对象
                    //获取HttpsURLConnection对象
                    HttpsURLConnection conn= (HttpsURLConnection) url.openConnection();
                    conn.setConnectTimeout(5000);//设置超时时间（毫秒）
                    //判断网络访问是否成功
                    if(conn.getResponseCode()==HTTP_OK){
                        //获取输入流
                        InputStream stream = conn.getInputStream();
                        //从流中获取字符串
                        String html= StreamUtil.stream2String(stream);
                        Message msg=new Message();
                        msg.obj=html;
                        handler.sendMessage(msg);
                    }
                    conn.disconnect();//关闭连接
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }.start();
    }
}